package com.hmall.vo;

import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author zt
 * @Date 2018/11/5 15:01
 * @Param
 * @return
 * @Description //
 */
@Data
public class PageVo<T> {
	/**
	 * 默认值
	 */
	static final    Integer MIN=1;
	static final    Integer MAX=8;
	/**
	 * 当前页
	 */
	private Integer pageNo;
	/**
	 * 每个页有多少行数据
	 */
	private Integer pageSize;
	/**
	 * 总数量
	 */
	private Integer totalRecord;
	/**
	 * 数据
	 */
	private  List<T> data;
	private Integer startIndex;
	/**
	 * 共多少页
	 */
	private Integer totalPages;
	/**
	 * 分页显示的页数
	 */
	private Integer start;
	private Integer end;
	private boolean hasNextPage;
	private List<Integer> indexList=new ArrayList<Integer>();
	public PageVo(){

	}
	public PageVo(Integer pageNo, Integer pageSize, Integer totalRecord, List<T> data) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.totalRecord = totalRecord;
        //说明页码和总数可以整除
        if (totalRecord%pageSize==0) {
            totalPages=totalRecord/pageSize;
        }else {
            totalPages=totalRecord/pageSize+1;
        }
		this.data=data;
        if(pageNo>=totalPages) {
            hasNextPage = false;
		//	pageNo=totalPages;
			return;
        } else{
            hasNextPage = true;
        }


		start=MIN;
		end=MAX;

		//  如果总页数小于5 end=总页数
		if (totalPages<=MAX) {
			end=totalPages;
		}else {

			 // 页码改变
			start=pageNo-4;
			end=pageNo+4;

			//如果开始小于0 恢复初始
			if (start<=0) {
				start=MIN;
				end=MAX;
				/**
				 * 如果end大于总页码
				 */
			}if (end>totalPages) {
				/**
				 * 赋值开始和截止
				 */
				end=totalPages;
				start=totalPages-MAX;
			}
		}
		//将页码存入页码集合中[3,4,5]
		for (int i = start; i <= end; i++) {
			indexList.add(i);
		}
	}


	@Override
	public String toString() {
		return "PageVo{" +
				"pageNo=" + pageNo +
				", pageSize=" + pageSize +
				", totalRecord=" + totalRecord +
				", data=" + data +
				", startIndex=" + startIndex +
				", totalPages=" + totalPages +
				", start=" + start +
				", end=" + end +
				", hasNextPage=" + hasNextPage +
				", indexList=" + indexList +
				'}';
	}
}
